﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace SPVisualDev.VSAddin.UserControls
{
    public class FarmExplorerObjectWrapper
    {
        public FarmExplorerObjectWrapper(IDictionary<string, string> Properties)
        {
            this.ObjectProperties = Properties;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Object type"), RefreshProperties(RefreshProperties.None), ReadOnly(true)]
        public string Type
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("URL"), RefreshProperties(RefreshProperties.None), ReadOnly(true)]
        public string URL
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DisplayName("Object properties"), DescriptionAttribute("Object properties"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ExpandablePropertyCollection))]
        public object ObjectProperties
        {
            get;
            set;
        }

        public class ExpandablePropertyCollection : ExpandableObjectConverter
        {
            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                IDictionary<string, string> dict = value as IDictionary<string, string>;

                List<PropertyDescriptor> list = new List<PropertyDescriptor>();
                foreach (KeyValuePair<string, string> kvp in dict)
                {
                    ExpandablePropertyDescriptor p = new ExpandablePropertyDescriptor(kvp.Key, attributes);

                    list.Add(p);
                }

                return new PropertyDescriptorCollection(list.ToArray());
            }

            public override string ToString()
            {
                return "(expand)";
            }
        }

        public class ExpandablePropertyDescriptor : PropertyDescriptor
        {
            public ExpandablePropertyDescriptor(string Name, Attribute[] attributes)
                : base(Name, attributes)
            {
            }

            public override bool CanResetValue(object component)
            {
                return false;
            }

            public override Type ComponentType
            {
                get { throw new NotImplementedException(); }
            }

            public override object GetValue(object component)
            {
                IDictionary<string, string> dict = component as IDictionary<string, string>;

                if (dict.ContainsKey(this.Name))
                    return dict[this.Name];
                else
                    return null;
            }

            public override bool IsReadOnly
            {
                get { return true; }
            }

            public override Type PropertyType
            {
                get { return typeof(string); }
            }

            public override void ResetValue(object component)
            {
            }

            public override void SetValue(object component, object value)
            {
            }

            public override bool ShouldSerializeValue(object component)
            {
                return false;
            }
        }
    }
}
